package edu.spec.student.service.impl;

import edu.spec.student.dao.iface.IRubricDao;
import edu.spec.student.dao.iface.IStudentDao;
import edu.spec.student.dao.iface.IStudentMentalDao;
import edu.spec.student.dao.iface.IStudentRubricDao;
import edu.spec.student.model.Student;
import edu.spec.student.model.mental.Rubric;
import edu.spec.student.model.mental.StudentMental;
import edu.spec.student.model.mental.StudentRubric;
import edu.spec.student.service.iface.IStudentRubricService;
import edu.spec.basic.model.enumeration.FinishStatus;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.Date;

@Service
public class StudentRubricService implements IStudentRubricService {

    @Inject
    private IRubricDao rubricDao;
    @Inject
    private IStudentRubricDao studentRubricDao;
    @Inject
    private IStudentDao studentDao;
    @Inject
    private IStudentMentalDao studentMentalDao;

    @Override
    public void finish(Integer studentId, Integer mentalId, String[] array) {
        Student s = studentDao.load(studentId);
        Integer totalScore = 0;
        // 保存小题情况
        for(String l : array){
            String[] group = l.split("-");
            Integer rId = Integer.parseInt(group[0]);
            Integer value = Integer.parseInt(group[1]);
            Integer score = Integer.parseInt(group[2]);
            Rubric r = rubricDao.load(rId);
            StudentRubric sr = new StudentRubric(s, r, value, score);
            studentRubricDao.add(sr);
            totalScore += score;
        }
        // 更新StudentMental
        StudentMental sm = studentMentalDao.loadByStudentAndMental(studentId, mentalId);
        sm.setScore(totalScore);
        sm.setFinishDate(new Date());
        sm.setFinishStatus(FinishStatus.FINISHED);
        studentMentalDao.update(sm);
    }

}
